/**
 * 
 */
package javavis.jip2d.functions;

import javavis.base.ImageType;
import javavis.base.JIPException;
import javavis.base.parameter.JIPParamInt;
import javavis.jip2d.base.FunctionGroup;
import javavis.jip2d.base.JIPFunction;
import javavis.jip2d.base.JIPImage;
import javavis.jip2d.base.bitmaps.JIPBmpByte;
import javavis.jip2d.base.bitmaps.JIPBmpColor;

/**
 * @author asc
 *
 */
public class FPrueba extends JIPFunction {

	/**
	 * 
	 */
	public FPrueba() {
		name = "FPrueba";
		description = "mi primera funcion javaVis";
		groupFunc = FunctionGroup.Others;
		
		JIPParamInt p1 = new JIPParamInt("Valor", false, true);
		p1.setDescription("Cambia el color de la imagen");
		p1.setDefault(25);
		
		addParam(p1);
	}

	/* (non-Javadoc)
	 * @see javavis.jip2d.base.JIPFunction#processImg(javavis.jip2d.base.JIPImage)
	 */
	@Override
	public JIPImage processImg(JIPImage img) throws JIPException {
		
		if(img.getType() != ImageType.COLOR) throw new JIPException("Se esperaba una imagen a color\n");
		
		JIPBmpColor ColorImag = (JIPBmpColor) img;
		
		FColorToGray fcg = new FColorToGray();
		fcg.setParamValue("gray", "BYTE");
		JIPBmpByte byteImg = (JIPBmpByte) fcg.processImg(ColorImag);
		
		int val=getParamValueInt("Valor");
		System.out.print("El valor es : " + String.valueOf(val) + "\n");

		//Constantes
		int alto = byteImg.getHeight() >> 1;
		int ancho = byteImg.getWidth() >> 1;
		
		//1er cuarto
		{
			for (int i = 0; i < ancho; i++) {
				for (int j = 0; j < alto; j++) {
					byteImg.setPixel(i, j, byteImg.getPixel(i, j)+getParamValueInt("Valor"));
				}
			}
		}
		
		//4cuarto
		int Principio=alto*byteImg.getWidth();
		double[] imggenFinal=byteImg.getAllPixels();
		{
			for (int i = Principio; i < imggenFinal.length; i++) {
				imggenFinal[i] -= getParamValueInt("Valor");
				
				if(i%ancho == 0)i+=ancho;
			}
		}
		byteImg.setAllPixels(imggenFinal);
		
		return byteImg;
	}

}
